package cfca.seal.dao.main.impl;

import cfca.seal.bean.AdminUser;
import cfca.seal.common.Page;
import cfca.seal.dao.PageDao;
import cfca.seal.dao.SmartDao;
import cfca.seal.dao.main.IAdminUserDao;
import cfca.seal.exception.CodeException;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class AdminUserDao
  implements IAdminUserDao
{
  private static String tableName = "管理员";
  private static final Logger log = LoggerFactory.getLogger("web_sys");

  @Resource
  private SmartDao smartDao;

  @Resource
  private PageDao pageDao;

  @Override
  public boolean insert(AdminUser manager) throws CodeException { try { String sql = "INSERT INTO ADMIN_USER (ID, NAME, REAL_NAME, PASSWORD, USER_TYPE, CERT_SN, CERT_DN, ORGANIZATION_ID, REMARK, LOGIN_ERROR_TIMES, CREATE_TIME) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

      Object[] args = { manager.getId(), manager.getName(), manager.getRealName(), manager.getPassword(), Integer.valueOf(manager.getUserType()), manager.getCertSn(), manager.getCertDn(), manager.getOrganizationId(), manager.getRemark(), Integer.valueOf(manager.getLoginErrorTimes()), new Date() };

      log.debug("insert admin_user sql: " + sql + args);
      int i = this.smartDao.update(sql, args);
      return i > 0;
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1002", tableName + "表插入失败", e);
    } }

  @Override
  public boolean update(AdminUser manager) throws CodeException
  {
    try
    {
      String sql = "UPDATE ADMIN_USER SET REAL_NAME = ?, PASSWORD = ?, USER_TYPE= ? , CERT_SN = ?, CERT_DN = ?, ORGANIZATION_ID = ?, REMARK = ?, LOGIN_ERROR_TIMES = ? WHERE ID = ?";

      Object[] args = { manager.getRealName(), manager.getPassword(), Integer.valueOf(manager.getUserType()), manager.getCertSn(), manager.getCertDn(), manager.getOrganizationId(), manager.getRemark(), Integer.valueOf(manager.getLoginErrorTimes()), manager.getId() };

      log.debug("update admin_user sql by id: " + sql + args);
      int i = this.smartDao.update(sql, args);
      return i > 0;
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1003", tableName + "表更新失败", e);
    }
  }

  @Override
  public AdminUser getManagerById(String id) throws CodeException
  {
    try {
      String sql = "SELECT * FROM ADMIN_USER WHERE ID = ?";
      Object[] args = { id };
      log.debug("select admin_user sql by Id: " + sql + args);
      return (AdminUser)this.smartDao.getBean(AdminUser.class, sql, args);
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1001", tableName + "表检索失败", e);
    }
  }

  @Override
  public AdminUser getManagerByUsername(String username) throws CodeException
  {
    try {
      String sql = "SELECT * FROM ADMIN_USER WHERE NAME = ?";
      Object[] args = { username };
      log.debug("select admin_user sql by name: " + sql + args);
      return (AdminUser)this.smartDao.getBean(AdminUser.class, sql, args);
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1001", tableName + "表检索失败", e);
    }
  }

  @Override
  public List<AdminUser> getAll() throws CodeException
  {
    try {
      String sql = "SELECT * FROM ADMIN_USER";
      log.debug("select all admin_user sql : " + sql);
      return this.smartDao.getList(AdminUser.class, sql);
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1001", tableName + "表检索失败", e);
    }
  }

  @Override
  public boolean deleteById(String id) throws CodeException
  {
    try {
      String sql = "DELETE FROM ADMIN_USER WHERE ID = ?";
      Object[] args = { id };
      log.debug("delete admin_user sql by id: " + sql + args);
      int i = this.smartDao.update(sql, args);
      return i > 0;
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1004", tableName + "表删除失败", e);
    }
  }

  @Override
  public boolean loginUpdate(AdminUser manager) throws CodeException
  {
    try {
      String sql = "UPDATE ADMIN_USER SET LAST_LOGIN_DATE = ?, LOGIN_ERROR_TIMES = ? WHERE ID = ?";
      Object[] args = { manager.getLastLoginDate(), Integer.valueOf(manager.getLoginErrorTimes()), manager.getId() };
      log.debug("update admin_user sql by id: " + sql + args);
      int i = this.smartDao.update(sql, args);
      return i > 0;
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1003", tableName + "表更新失败", e);
    }
  }

  @Override
  public Page<AdminUser> getPage(String pageno) throws CodeException
  {
    try {
      return this.pageDao.dispage(AdminUser.class, "ADMIN_USER", null, new Object[0], pageno, 20);
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1001", tableName + "表检索失败", e);
    }
  }

  @Override
  public AdminUser getAdminUser(String certDN, String certSN) throws CodeException
  {
    try {
      String sql = "SELECT * FROM ADMIN_USER WHERE CERT_DN = ?  AND CERT_SN=?";
      Object[] args = { certDN, certSN };
      log.debug("select admin_user sql by dn sn: " + sql + args);
      return (AdminUser)this.smartDao.getBean(AdminUser.class, sql, args);
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1001", tableName + "表检索失败", e);
    }
  }
}